/**
 * @param {number[]} nums
 * @return {number}
 */
 var subArrayRanges = function(nums) {
  const n = nums.length
  let ans = 0
  // let max = [...nums], min = [...nums]
  // for(let i = 1; i<n; i++){
  //   max[i] = Math.max(max[i], max[i-1])
  //   min[i] = Math.min(min[i], min[i-1])
  // }
  const handler = (i, j) => {
    let maxNum = -Infinity, minNum = Infinity
    for(let k = i; k<=j; k++){
      maxNum = Math.max(maxNum, nums[k])
      minNum = Math.min(minNum, nums[k])
    }
    ans += maxNum - minNum
  }
  for(let i = 0; i<n; i++){
    for(let j = i; j<n; j++){
      handler(i, j)
    }
  }
  return ans
};